var config = require('../../config/config.js');
var model = require('../../model/model.js')
var show = false;
var item = {};
var index = 0;
Page({
  data: {
    info_list: {},
    items: [
      { name: "0", value: "企业", id: 0, checked: true },
      { name: "1", value: "家庭", id: 1 },
    ],
    value: '',
    name: "",
    mobile: "",
    addreValue: 0,
    address: "",
    province_id: "110000",
    city_id: "110100",
    area_id: '',
    id: '',
    type: '',
    province: '北京市',
    city: '市辖区',
    county: '',
    area_info: '',
    item: {
      show: show
    },
  },
  radioChange: function (e) {
    this.setData({
      value: e.detail.value
    })
  },
  getInfo: function (e) {
    var that = this;
    var member_info = (wx.getStorageSync('member_info'));
    wx.request({
      url: config.member_info,
      data: {
        aid: member_info.aid,
        mid: member_info.mid,
        sid: member_info.sid,
      },
      method: 'GET',
      success: function (res) {
        that.setData({
          info_list: res.data.data.info,
          province: res.data.data.info.province,
          city: res.data.data.info.city,
          county: res.data.data.info.area,
          type: res.data.data.info.type
        })
      },
      fail: function () {
      },
    })
  },
  formSubmit: function (e) {
    var warn = "";
    var that = this;
    var flag = false;
    if (e.detail.value.name == "") {
      warn = "请填写您的姓名！";
    } else if (e.detail.value.mobile == "") {
      warn = "请填写您的手机号！";
    } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value.mobile))) {
      warn = "手机号格式有误";
    } else if (this.data.value== 0 && e.detail.value.company_name == ''){
        warn = "请填写你的公司名称";
    } else if (e.detail.value.address == "") {
      warn = "请输入您的具体地址";
    } else {
      flag = true;
    }
    if (flag == false) {
      wx.showModal({
        title: '提示',
        content: warn
      })
      return
    }
    //提交表单内容
    var that = this;
    var member_info = (wx.getStorageSync('member_info'));
    wx.request({
      url: config.member_add_information,
      method: 'POST',
      data: {
        name: e.detail.value.name,
        mobile: e.detail.value.mobile,
        address: e.detail.value.address,
        aid: member_info.aid,
        mid: member_info.mid,
        sid: member_info.sid,
        province_id: this.data.province_id,
        city_id: this.data.city_id,
        area_id: this.data.area_id,
        area_info: this.data.province + ' ' + this.data.city + ' ' + this.data.county,
        type: this.data.value,
        company_name: e.detail.value.company_name,
      },
      success: function (res) {
        // var mobile = that.data.mobile; 
        // mobile = e.detail.value.mobile;
        var member_info = (wx.getStorageSync('member_info'));
        member_info.mobile = e.detail.value.mobile;
        if (res.data.status) {
          wx.setStorageSync('member_info', member_info);
          wx.navigateBack({
            delta: 1
          })
        }
      },
    })
  },
  /**
 * 生命周期函数--监听页面加载
 */
  onLoad: function (options) {
  
    this.getInfo()
    if (options.type) {
      this.setData({
        name: options.name,
        address: options.address,
        area_info: options.area_info,
        city: options.area_info[1],
        county: options.area_info[2],
        mobile: options.mobile,
        id: options.id
      })
      var area_info = this.data.area_info.split(" ")
      this.setData({
        area_info: area_info,
        province: area_info[0],
        city: area_info[1],
        county: area_info[2],
      })

    }
  },

  //生命周期函数--监听页面初次渲染完成
  onReady: function (e) {
    var that = this;
    //请求数据
    model.updateAreaData(that, 0, e);
  },
  //点击选择城市按钮显示picker-view
  translate: function (e) {
    model.animationEvents(this, 0, true, 400);
  },
  //隐藏picker-view
  hiddenFloatView: function (e) {
    model.animationEvents(this, 200, false, 400);
  },
  //滑动事件
  bindChange: function (e) {
    model.updateAreaData(this, 1, e);
    item = this.data.item;
    this.setData({
      province: item.provinces[item.value[0]].name,
      city: item.citys[item.value[1]].name,
      county: item.countys[item.value[2]].name,
      province_id: item.provinces[item.value[0]].code,
      city_id: item.citys[item.value[1]].code,
      area_id: item.countys[item.value[2]].code,
    });
  },
  onReachBottom: function () {
  },
  nono: function () { },

})